<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add(9)">丢件登记</el-button>
				<el-button type="primary" icon="el-icon-plus" @click="add(999)">买家问题件</el-button>
				<el-button type="primary" icon="el-icon-plus" @click="add(3)">拦截</el-button>
				<el-button type="primary" icon="el-icon-plus" @click="add(5)">取消寄件</el-button>
				<el-button type="primary" icon="el-icon-plus" @click="add(1)">极速退款</el-button>
				
			</div>

			<div class="right-panel">
				<el-select v-model="searchOption.state" placeholder="请选择状态">
					<el-option label="全部状态" value=""></el-option>
					<el-option label="拦截" value="intercept"></el-option>
					<el-option label="取消寄件" value="cancel"></el-option>
					<el-option label="极速退款" value="speed"></el-option>
					<el-option label="丢件" value="lost"></el-option>
				</el-select>
				<div class="right-panel-search">
					<el-date-picker
				        v-model="searchOption.start_time"
				        type="datetime"
				        placeholder="开始时间"
				        format="YYYY-MM-DD"
				        value-format="YYYY-MM-DD"
				        date-format="MMM DD, YYYY"
				        time-format="HH:mm"
				      />
				    <el-date-picker
				        v-model="searchOption.end_time"
				        type="datetime"
				        placeholder="结束时间"
				        format="YYYY-MM-DD"
				        date-format="MMM DD, YYYY"
				        time-format="HH:mm"
				      />
					<el-input v-model="searchOption.express_number" placeholder="搜索快递号" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" @selection-change="selectionChange" stripe>
				<el-table-column label="类型" prop="name" width="130">
					<template #default="scope">
						<el-tag  style="font-weight: bold; font-size: 14px" type="error" v-if="scope.row.type == 1">极速退款登记</el-tag>
						<el-tag  style="font-weight: bold; font-size: 14px" type="success" v-if="scope.row.type == 2">极速退款收到</el-tag>
						<el-tag  style="font-weight: bold; font-size: 14px" type="waring" v-if="scope.row.type == 3">拦截登记</el-tag>
						<el-tag  style="font-weight: bold; font-size: 14px" type="success" v-if="scope.row.type == 4">拦截收到</el-tag>
						<el-tag  style="font-weight: bold; font-size: 14px" type="waring" v-if="scope.row.type == 5">取消寄件登记</el-tag>
						<el-tag  style="font-weight: bold; font-size: 14px" type="success" v-if="scope.row.type == 6">取消寄件收到</el-tag>
						<el-tag  style="font-weight: bold; font-size: 14px" v-if="scope.row.type == 7">退件收到</el-tag>
						<el-tag  style="font-weight: bold; font-size: 14px" v-if="scope.row.type == 9">丢件登记</el-tag>
						<el-tag  style="font-weight: bold; font-size: 14px" type="danger" v-if="scope.row.type == 999">买家问题件</el-tag>
					</template>
				</el-table-column>
				
				<el-table-column label="快递单号" prop="express_number" width="150">
					<template #default="scope">
						<div class="right-panel">
							<el-link :href="scope.row.search_url" target="_blank">{{scope.row.express_number}}</el-link>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="快捷操作" prop="express_number" width="200">
					<template #default="scope">
						<div class="right-panel">
							<el-button v-copy="scope.row.express_number" size="small" type="primary">复制单号</el-button>
							<el-button v-copy="scope.row.script" size="small" type="primary">复制话术</el-button>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="时间" prop="created_at" width="150"></el-table-column>
				<el-table-column label="金额" prop="amount" width="150"></el-table-column>
				
				<el-table-column label="操作" fixed="right" align="right" width="100">
					<template #default="scope">
						<el-button type="primary" plain size="small" @click="table_edit(scope.row)">编辑</el-button>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

	<el-drawer v-model="dialog.info" :size="800" title="详细" direction="rtl" destroy-on-close>
		<info ref="infoDialog"></info>
	</el-drawer>

</template>

<script>
	import saveDialog from './save'
	import info from './info'

	export default {
		name: 'listCrud',
		components: {
			saveDialog,
			info
		},
		data() {
			return {
				dialog:{
					save: false,
					info: false
				},
				list: {
					apiObj: this.$API.refund.list
				},
				search: {},
				selection: [],
				searchOption: {
					express_number: '',
					start_time: '',
					end_time:''
				},
			}
		},
		mounted() {
			
		},
		methods: {
			upsearch(page = 1) {
				this.$refs.table.upData(this.searchOption, page)		
			},
			//窗口新增
			add(type){
				this.dialog.save = true
				var data = {
					type: type
				}
				this.$nextTick(() => {
					this.$refs.saveDialog.open().setData(data)
				})
			},
			//窗口编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//页面新增
			addPage(){
				this.$router.push({
					path: '/template/list/crud/detail',
				})
			},
			//页面编辑
			table_edit_page(row){
				this.$router.push({
					path: '/template/list/crud/detail',
					query: {
						id: row.id
					}
				})
			},
			//查看
			table_show(row){
				this.dialog.info = true
				this.$nextTick(() => {
					this.$refs.infoDialog.setData(row)
				})
			},
			//删除明细
			async table_del(row, index){
				var reqData = {id: row.id}
				var res = await this.$API.demo.post.post(reqData);
				if(res.code == 200){
					this.$refs.table.removeIndex(index)
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				var confirmRes = await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '删除',
					confirmButtonClass: 'el-button--danger'
				}).catch(() => {})

				if(!confirmRes){
					return false
				}

				var ids = this.selection.map(v => v.id)
				this.$refs.table.removeKeys(ids)
				this.$message.success("操作成功")

			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				//为了减少网络请求，直接变更表格内存数据
				if(mode=='add'){
					this.$refs.table.unshiftRow(data)
				}else if(mode=='edit'){
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			}
		}
	}
</script>

<style>
</style>
